import java.nio.charset.IllegalCharsetNameException;
import java.util.InputMismatchException;
import java.util.Scanner;
class NameException extends RuntimeException{
public  NameException(String message){
    super(message);
}
}
class  PasswordException extends RuntimeException{
    public PasswordException(String message){
        super(message);
    }
}
public class TestDemo {
    public static void main1(String[] args) {
        int[]array={1,2,3};
        System.out.println(array[5]);
        System.out.println("其它功能");
    }

    public static void main2(String[] args) {
        int[]array={1,2,3};
        try{
            System.out.println(array[5]);
            System.out.println("haha!");
        }
       catch(ArrayIndexOutOfBoundsException e) {
            e.printStackTrace();
           System.out.println("我捕捉到了一个数组越界异常，这里开始处理异常了");
       }
        System.out.println("hhhh!!");
    }

    public static void main3(String[] args) {
        Scanner scanner=new Scanner(System.in);
        try{
            int n=scanner.nextInt();
            System.out.println(10/n);
        }catch(InputMismatchException e){
            e.printStackTrace();
            System.out.println("输入有误！");
        }catch (ArithmeticException e){
            e.printStackTrace();
            System.out.println("算术异常，可能0作为了除数");
        }finally {
            //一般用作资源的关闭
            scanner.close();
            System.out.println("finally执行了!");
        }
        scanner.close();
    }

    public static void func4(int x) throws RuntimeException{
        if (x==10){
           // System.out.println(10/x);
            throw new ArithmeticException("/ by 0");
        }
    }

    public static void main4(String[] args) {
        try{
            func4(0);
        }catch (ArithmeticException e){
            e.printStackTrace();
        }
    }
    private static final String name="bit";
    private static final String password="123";
    public static void login(String name,String password)throws NameException,PasswordException{
        if (!TestDemo.name.equals(name)){
            throw new NameException("用户名错误");
        }
        if (!TestDemo.password.equals(password)){
            throw new PasswordException("密码错误");
        }
    }

    public static void main(String[] args) {
        try{
            login("bit","1234");
        }catch(NameException e){
            System.out.println("用户名错误！");
        }catch (PasswordException e){
e.printStackTrace();
            System.out.println("密码错误了！");;
        }finally {
            System.out.println("finally执行了！");
        }
    }

    public static void main5(String[] args) {
        int i=0;
        while(i<10){
            try{
                if (i<10){
                    throw new Exception();
                }
            }catch (Exception e){
                e.printStackTrace();
                System.out.println("尝试连接网络"+i+"次..........");
                i++;
            }
        }
        System.out.println("终于有网了。程序继续执行！！！");
    }
}























